package com.aaa.mbs.controller;

import com.aaa.common.util.Result;
import com.aaa.common.util.ResultStatus;
import com.aaa.mbs.entity.MemberLevel;
import com.aaa.mbs.service.MemberLevelService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (MemberLevel)表控制层
 *
 * @author yuxiujie
 * @since 2021-01-23 10:17:20
 */
@RestController
@RequestMapping("memberLevel")
@ApiOperation("memberLevel控制器")
public class MemberLevelController {
    /**
     * 服务对象
     */
    @Resource
    private MemberLevelService memberLevelService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("单个对象查询")
    public Result<MemberLevel> selectOne(Long id) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberLevelService.queryById(id));
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public Result<PageInfo<MemberLevel>> queryAll(Integer pageNo, Integer pageSize, @RequestBody MemberLevel memberLevel) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberLevelService.queryAll(pageNo, pageSize, memberLevel));
    }

    /**
     * 新增数据
     *
     * @param memberLevel 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public Result<MemberLevel> insert(@RequestBody MemberLevel memberLevel) {
        MemberLevel insert = this.memberLevelService.insert(memberLevel);
        if (insert != null) {
            return new Result<>(ResultStatus.SUCCESS.getCode(),
                    ResultStatus.SUCCESS.getMessage(),
                    insert);
        } else {
            return new Result<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMessage(), null);
        }
    }

    /**
     * 修改数据
     *
     * @param memberLevel 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Result<MemberLevel> update(@RequestBody MemberLevel memberLevel) {
        MemberLevel update = memberLevelService.update(memberLevel);
        if (update != null) {
            return new Result<>(ResultStatus.SUCCESS.getCode(),
                    ResultStatus.SUCCESS.getMessage(),
                    update);
        }
        return new Result<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMessage(), null);
    }

    /**
     * 通过主键删除数据
     *
     * @param memberLevelId 主键
     * @return 是否成功
     */
    @DeleteMapping("deleteById")
    public Result<Boolean> deleteById(Long memberLevelId) {
        boolean b = this.memberLevelService.deleteById(memberLevelId);
        if (b) {
            return new Result<>(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMessage(), true);
        }else {
            return new Result<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMessage(), false);
        }
    }
}